package modules.lobby
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.load.Load;
	import modules.load.LoadEvent;
	import modules.lobby.view.BalanceView;
	import modules.lobby.view.JackpotNumViewManager;
	import modules.lobby.view.MailView;
	import modules.lobby.view.PlayerInfoView;
	import modules.lobby.view.roomPlayerInfoUI.RoomPlayerInfo;
	import modules.lobby.view.tableListUI.TableListUIManager;
	

	public class LobbyUIManager extends ViewManager
	{
		private var _loading:Boolean = false;

		private var isLoad:Boolean = false;

		private var loadingPaths:Array;

		private var jackpotNumViewManager:JackpotNumViewManager;
		private var playerInfoView:PlayerInfoView;
		private var balanceView:BalanceView;
		private var mailView:MailView;
		private var roomPlayerInfoView:RoomPlayerInfo;

		private var roomListUIManager:TableListUIManager;
		
		
		public function LobbyUIManager()
		{
			dispatcher.addEventListener(LobbyEvent.LOBBY_SHOW, onGameEvent);
			
			/** 通知用户信息 */
			dispatcher.addEventListener(HSProto.HSID_USER_INFO_NTF.toString(), onGameEvent);
			
			dispatcher.addEventListener(LobbyEvent.LOBBY_CLOSE, onGameEvent);
		}

		public function get loading():Boolean
		{
			return _loading;
		}

		public function set loading(value:Boolean):void
		{
			_loading = value;
			if (_loading)
				dispatcher.addEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
			else
				dispatcher.removeEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);

		}

		override protected function init():void
		{
			if (loading)
				return;

			if (!isLoad)
			{
				loading = true;
				loadingPaths = [Lobby.viewPath];
				dispatcher.dispatchEvent(new LoadEvent(LoadEvent.LOAD_RESOURCE, {urls: loadingPaths}));
				return;
			}

			mainUI = Load.getInstance("fla.ui.LobbyUI");
			
			//jackpot
			jackpotNumViewManager = new JackpotNumViewManager(mainUI.jackpotMC);
			
			roomListUIManager = new TableListUIManager(mainUI.lobbyRoomListUI);
			
			//playerInfo
			playerInfoView = new PlayerInfoView(mainUI.playerInfoView);
			//balance
			var balanceMc:MovieClip = Load.getInstance("fla.ui.balance");
			balanceView = new BalanceView(balanceMc);
			//mail
			var mailUI:MovieClip = Load.getInstance("fla.ui.mailUI");
			mailView = new MailView(mailUI);
			//roomplayerInfo
			roomPlayerInfoView = new RoomPlayerInfo(mainUI.roomPlayerInfoUI);
			
			mainUI.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainUI.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

			mainUI.x = (UIAllRefer.stage.stageWidth - mainUI.width) / 2;
			mainUI.y = (UIAllRefer.stage.stageHeight - mainUI.height) / 2;

			Lobby.isInit = true;

			show();
		}

		override protected function show():void
		{
			if (!Lobby.isInit)
			{
				init();
				return;
			}
			
			updateView();

			Lobby.isShow = true;

			UIAllRefer.contentLayer.addChild(mainUI);
			
			dispatcher.dispatchEvent(new GameEvent(GameEvent.SHOW_LOBBY));
		}

		override protected function updateView():void
		{

		}

		override protected function close():void
		{
			if (mainUI.parent)
				mainUI.parent.removeChild(mainUI);

			Lobby.isShow = false;
		}

		override protected function onAddToStage(event:Event):void
		{
			
		}

		override protected function onRemovedFromStage(event:Event):void
		{

		}

		/**
		 * 处理登陆成功
		 * @param event
		 */
		private function onLobbySucceed(event:LobbyEvent):void
		{
			updateView();
			close();
		}

		/**
		 * 处理登陆失败
		 * @param event
		 */
		private function onLobbyFail(event:LobbyEvent):void
		{
			updateView();

			var data:Object = event.data;
			if (data && data.info)
			{
				mainUI.infoTxt.text = data.info;
			}
			else
			{
				mainUI.infoTxt.text = "登陆失败";
			}
		}

		protected function onGameEvent(event:GameEvent):void
		{
			switch (event.type)
			{
				case LobbyEvent.LOBBY_SHOW:
					show();
					break;
				case HSProto.HSID_USER_INFO_NTF.toString():
					show();
					break;
				case LobbyEvent.LOBBY_CLOSE:
					close();
					break;
			}
		}

		protected function onClick(event:MouseEvent):void
		{
			switch (event.currentTarget)
			{
				case mainUI.LobbyBtn:

					break;
			}
		}

		private function onLoadSingleComplete(event:LoadEvent):void
		{
			if (loadingPaths.length == 0)
				return;
			var loadingItem:LoadingItem = event.data;
			var index:int = loadingPaths.indexOf(loadingItem.url.url);
			if (index != -1)
			{
				loadingPaths.splice(index, 1);
				if (loadingPaths.length == 0)
				{
					loading = false;
					isLoad = true;
					init();
				}
			}
		}
		
		public function get mainView():Sprite
		{
			return mainUI;
		}
	}
}
